const fs = require('fs')
const path = require('path')

fs.readFile(path.join(__dirname, 'index.html'), 'utf-8',(err,data) => {
    // console.log('读取文件成功', data)
    if (err) {
        return console.log('读取文件失败', err)
    }
    const css = data.match(/<style>([\s\S]*)<\/style>/)[1]
    const js = data.match(/<script>([\s\S]*)<\/script>/)[1]
    fs.writeFile(path.join(__dirname, 'index.css'), css, (err) => {
        if (err) {
            return console.log('写入css失败', err)
        }
        console.log('写入css成功')
    })
    fs.writeFile(path.join(__dirname, 'index.js'), js, (err) => {
        if (err) {
            return console.log('写入js失败', err)
        }
        console.log('写入js成功')
    })
    let newData = data.replace(/<style>[\s\S]*<\/style>/, '<link rel="stylesheet" href="./index.css">')
    newData = newData.replace(/<script>[\s\S]*<\/script>/, '<script src="./index.js"></script>')
    fs.writeFile(path.join(__dirname, 'index1.html'), newData, (err) => {
        if (err) {
            return console.log('写入html失败', err)
        }
        console.log('写入html成功')
    })
})